<template>
  <div class="rule-box">
    <el-form label-width="100px" ref="editForm">
      <el-form-item label="">
        <p><span class="el-icon-warning infospan"></span>设置区角对应评分规则，管理者对应评分规则进行学生打分</p>
      </el-form-item>
      <el-form-item label="学习品质:">
        <el-select placeholder="请选择学习品质" size="small" v-model="study_ids" multiple>
          <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in selectList"
              :ksy="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评分规则:" v-if="!cornerId || (cornerId&&!assessList.length)">
        <v-selectpage :data="selectpagelist" key-field="id" show-field="title" class="form-control"
                      :tb-columns="columns" :multiple="true" v-model="domain_ids" @values="multipleValues"
                      placeholder="选择评分规则" title="选择评分规则" :page-size="10" ref="selectPoint">
        </v-selectpage>
        <div class="point-body">
          <div class="point-list" v-for="(item,index) in multipleValuesList">
            <div class="point-list-title">
              <p class="point-list-title-left">{{ item.title }}</p>
              <v-selectpage :data="scoreList" key-field="id" show-field="name" class="form-control" :multiple="true"
                            v-model="item.scoreValue" @values="scoreListFn($event,index)" placeholder="选择评分等级"
                            title="设置等级及分值"></v-selectpage>
            </div>
            <div class="point-list-line">
              <div class="point-list-line-list" v-for="(pointItem,pointIndex) in item.scoreList">
                <span class="point-list-line-list-title">{{ pointItem.name }}</span>
                <div class="point-list-line-list-input">
                  <el-input v-model="pointItem.scoreValue" auto-complete="off" placeholder="设定分值"
                            size="mini" @input="getNewVal"></el-input>
                  分
                </div>
                <div class="point-list-line-list-input-desc">
                  <el-input v-model="pointItem.desc" auto-complete="off" placeholder="填写描述"
                            size="mini" @input="getNewVal" type="textarea" rows="2"></el-input>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="评分规则:" v-if="cornerId && assessList.length">
        <div class="point-body">
          <div class="point-list" v-for="(item,index) in assessList" :key="index">
            <div class="point-list-title">
              <p class="point-list-title-left">{{ item.assess.title }}</p>
              <span class="point-list-title-right" @click="getLinebox(index)">{{ item.status ? '收起' : '展开' }}</span>
            </div>
            <div class="point-list-line" v-if="item.status">
              <div class="point-list-line-list" v-for="(pointItem,pointIndex) in item.fraction" :key="pointItem.id">
                <span class="point-list-line-list-title">{{ pointItem.name }}</span>
                <div class="point-list-line-list-input">
                  {{ pointItem.score }}分
                </div>
                <div class="point-list-line-list-input-desc desc-be">
                  {{ pointItem.desc }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {qualityIndex} from '@/api/admin/acquisition'

export default {
  components: {},
  props: ['cornerId', 'assess','studyIds'],
  data() {
    return {
      editForm: {
        list: ''
      }, // 编辑界面数据
      // domain_ids
      domain_ids: '',
      columns: [
        {title: '区角评价维度', data: 'title'},
        {
          title: '领域关联',
          data: function (row) {
            let string_ = []
            for (let i in row.domain_ids) {
              string_.push(row.domain_ids[i].name)
            }
            return string_.join(',')
          }
        },
        {title: '描述', data: 'description'}
      ],
      multipleValuesList: [],
      newList: {},
      scoreList: [], // 评分分列表
      selectpagelist: [],
      is_assess: true,
      assessList: [],
      selectList: [],
      study_ids: [],
      cornerId: 0
    }
  },
  created() {
    this.getFielFn(); //
    this.getPointFn()
    this.getstudyPoint()
  },
  methods: {
    // 获取学习品质
    getstudyPoint(){
      qualityIndex({p:1}).then(res=>{
        this.selectList = res.list.data
      })
    },
    // 获取领域
    getFielFn() {
      this.$post(this.api.corner.attr.index, {
        p: 0
      }).then((response) => {
        if (response.code === 0) {
          this.selectpagelist = response.list
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    getPointFn() {
      this.$post(this.api.setting.cornerIndex, {
        p: 1
      }).then((response) => {
        if (response.code === 0) {
          this.scoreList = response.list.data
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    multipleValues(option) {
      console.log('multipleValues', option)
      console.log('this.multipleValuesList:', this.multipleValuesList)
      console.log('this.newList:', this.newList)
      let newArray = {}
      for (let i in option) {
        if (this.newList[option[i].id]) {
          option[i].scoreList = this.getJson(this.newList[option[i].id].scoreList)
          option[i].scoreValue = this.getJson(this.newList[option[i].id].scoreValue)
          option[i].desc = this.getJson(this.newList[option[i].id].desc)
        } else {
          option[i].scoreList = []
          option[i].scoreValue = ''
          option[i].desc = ''
        }
        newArray[option[i].id] = option[i] // 对比参数
      }
      this.newList = this.getJson(newArray)
      this.multipleValuesList = this.getJson(option)
      console.log('this.multipleValuesList123123:', this.multipleValuesList)
      console.log('this.newList123123:', this.newList)
    },
    // 分值更改
    scoreListFn(option, index) {
      // 不管添加删除，只要更改了就会变化 option
      console.log('呼呼：', this.newList, option, index)
      console.log('元数据：', this.multipleValuesList)
      let orderDetail_ = JSON.parse(JSON.stringify(this.multipleValuesList));
      let list = this.getJson(this.newList);
      let scoreList = orderDetail_[index].scoreList
      let score_id = []
      for (let i in option) {
        option[i].scoreValue = ''
        // option[i].desc = orderDetail_[index].description
        score_id.push(option[i].id)
        for(let item of scoreList){
          if(item.id===option[i].id){
            option[i].scoreValue = item.scoreValue
            option[i].desc = item.desc
          }
        }
      };

      // 需要判断是否已经存在内容
      orderDetail_[index].scoreList = JSON.parse(JSON.stringify(option));
      list[orderDetail_[index].id].scoreList = JSON.parse(JSON.stringify(option));
      list[orderDetail_[index].id].scoreValue = score_id.join(',')
      this.newList = this.getJson(list);
      this.multipleValuesList = Object.assign({}, this.multipleValuesList, orderDetail_);
      console.log('option:', option,this.newList, this.multipleValuesList[index]);
    },
    getLinebox(index) {
      this.assessList[index].status = !this.assessList[index].status
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getNewVal() {
      this.$emit('getModleVal', {data: this.getJson(this.multipleValuesList), type: 'multipleValuesList'})
    }
  },
  mounted() {
  },
  watch: {
    studyIds(){
      let a = this.studyIds.map(Number)
      console.log('aaaa:', a)
      this.study_ids = a
    },
    assess() {
      console.log('assess更新了ds', this.assess)
      let n = this
      let list = this.getJson(this.assess)
      for (let i in list) {
        list[i].status = false
      }
      this.$set(this, 'assessList', this.getJson(list));
      console.log('assess更新了ds', this.assess)

      // let list = this.getJson(this.assess)
      // let a = []
      // for (let i in list) {
      //   let b = {};
      //   let scoreList = []
      //   let d = []
      //   b.title = list[i].assess.title
      //   list[i].status = false
      //   for (let item of list[i].fraction) {
      //     let c = {}
      //     c.id = item.id
      //     c.name = item.name
      //     c.scoreValue = item.score
      //     c.desc = item.desc || ''
      //     d.push(item.id)
      //     scoreList.push(c)
      //   }
      //   b.scoreValue = d.join(',')
      //   b.scoreList = scoreList
      //   a.push(b)
      // }
      // this.$set(this, 'assessList', this.getJson(list));
      // this.multipleValuesList =this.getJson(a)
    },
    multipleValuesList() {
      console.log('this.multipleValuesList123:', this.multipleValuesList)
      this.$emit('getModleVal', {data: this.getJson(this.multipleValuesList), type: 'multipleValuesList'})
    },
    study_ids(){
      this.$emit('getModleVal', {data: this.getJson(this.study_ids), type: 'study_ids'})
    }
  }
}
</script>
<style scoped>
.point-list-title {
  border-bottom: 1px solid #fff;
}

.point-list-title-right {
  color: #409EFF;
  cursor: pointer;
}

span.infospan {
  color: #ff8600;
  margin-right: 5px;
}

.rule-box {

}
.point-list-line-list-input{
  width: 200px;
  margin-right: 20px;
}
.point-list-line-list-input-desc{
  flex: 1;
  padding: 4px 0;
}
.desc-be{
  line-height: 1.3;
}
</style>


